package com.syni.mdd.sjb.common.datatreaure.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DankCdpOrderTotal;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.customBo.CdpStatBo;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.mapper.DankCdpOrderTotalMapper;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.datatreaure.service.DankCdpOrderTotalService;
import com.syni.mdd.sjb.common.datatreaure.service.UserService;
import com.syni.mdd.sjb.common.datatreaure.entity.bo.dataBankBusiness.CdpUtilsBo;
import com.syni.mdd.sjb.common.datatreaure.utils.CDPUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcc
 * @since 2020-10-11
 */
@Service
public class DankCdpOrderTotalServiceImpl extends ServiceImpl<DankCdpOrderTotalMapper, DankCdpOrderTotal> implements DankCdpOrderTotalService {

    @Autowired
    DankCdpOrderTotalMapper dankCdpOrderTotalMapper;

    @Autowired
    UserService userService;

    @Override
    public Double countWeekTotalAmount(Integer userId) {
        Date nowTime = new Date();
        String startDate = DateTimeUtils.converDateToString(DateTimeUtils.getFirstDayOfWeek(nowTime),DateTimeUtils.DATE_PATTERN_DAY);
        String endTDate = DateTimeUtils.converDateToString(DateTimeUtils.getLastDayOfWeek(nowTime),DateTimeUtils.DATE_PATTERN_DAY);
        CdpUtilsBo cdpUtilsBo = new CdpUtilsBo();
        CDPUtils.orderOrderTotal(cdpUtilsBo,userService.getAccountIdByUserMap(userId),startDate,endTDate);
        return cdpUtilsBo.getTotalAmount();
    }

    @Override
    public Double countAllTotalAmount(Integer userId) {
        Date nowTime = new Date();
        String startDate = DateTimeUtils.converDateToString(DateTimeUtils.addMonth(nowTime,-12),DateTimeUtils.DATE_PATTERN_DAY);
        String endTDate = DateTimeUtils.converDateToString(DateTimeUtils.getLastDayOfWeek(nowTime),DateTimeUtils.DATE_PATTERN_DAY);
        CdpUtilsBo cdpUtilsBo = new CdpUtilsBo();
        CDPUtils.orderOrderTotal(cdpUtilsBo,userService.getAccountIdByUserMap(userId),"",endTDate);
        return cdpUtilsBo.getTotalAmount();
    }

    @Override
    public Map<String,Double> countTimeByGroup(Integer userId,Integer startTimestamp, Integer endTimestamp, String groupType) {
        Map<String,Double> returnData = new LinkedHashMap<>();
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("startTimestamp",startTimestamp);
        queryMap.put("endTimestamp",endTimestamp);
        queryMap.put("groupType",groupType);
        queryMap.put("userId",userId);
        List<CdpStatBo> data = dankCdpOrderTotalMapper.countTimeByGroup(queryMap);
        if(CollectionUtils.isNotEmpty(data)){
            for(CdpStatBo cdpStatBo : data){
                returnData.put(cdpStatBo.getDescTime(),cdpStatBo.getTotal());
            }
        }
        return returnData;
    }

    public void getYesDayData(CdpUtilsBo cdpUtilsBo,Integer userId,String target) {
        String postUrl = "/cdp/cdp-web-api/order/yesterday_target_total";
        Map<String,Object> params = new HashMap<>();
        params.put("accountId",userService.getAccountIdByUserMap(userId));
        params.put("target",target);
        Map<String,Object> resultMap = CDPUtils.commonCall(postUrl,params);
        if(ResultMapHelper.isSuccess(resultMap)){
            JSONArray dataMap = (JSONArray) resultMap.get("data");
            if(dataMap.size() > 0){
                for(int ii = 0;ii < dataMap.size();ii++){
                    JSONObject jsonObject = dataMap.getJSONObject(ii);
                    if(target.equals("orderAmount")){
                        Double values = Double.parseDouble(jsonObject.get("qValue").toString());
                        String time = jsonObject.get("hourTime").toString();
                        cdpUtilsBo.getYesLineData().put(time,values);
                    }
                }
            }
        }
        if(resultMap.get("status").equals("09") && "ff8080816a4d156a016a4e29e6b80006".equals(params.get("accountId"))){
            for(int ii = 0;ii < 24;ii++){
                Double values = Integer.valueOf((int)(Math.random() * 10)).doubleValue();
                String time = String.valueOf(ii);
                if(ii < 10){
                    time = "0" + ii;
                }
                cdpUtilsBo.getYesLineData().put(time,values);
            }
        }
    }

    @Override
    public void getOrderTotalTotalAmountByGroup(CdpUtilsBo cdpUtilsBo,
                                                               Integer userId, String searcDate, String endDate,String group) {
        Map<String,Double> returnMap = new HashMap<>();
        String postUrl = "/cdp/cdp-web-api/order/order-total";
        Map<String,Object> params = new HashMap<>();
        params.put("accountId",userService.getAccountIdByUserMap(userId));
        params.put("st",searcDate);
        params.put("et",endDate);
        params.put("totalDimension",group);
        params.put("channel","微信小商店");
        Map<String,Object> resultMap = CDPUtils.commonCall(postUrl,params);
        if(ResultMapHelper.isSuccess(resultMap)){
            Map<String,Object> dataMap = (Map<String, Object>) resultMap.get("data");
            if(dataMap.containsKey("totalAmount")){
                cdpUtilsBo.setTotalAmount(Double.parseDouble(dataMap.get("totalAmount").toString()));
            }
            if(dataMap.containsKey("orderQuantity")){
                cdpUtilsBo.setOrderQuantity(Integer.parseInt(dataMap.get("orderQuantity").toString()));
            }
            if(dataMap.containsKey("totalList")) {
                JSONArray jsonArray = (JSONArray) dataMap.get("totalList");
                for(int ii = 0;ii < jsonArray.size();ii++){
                    JSONObject jsonObject = jsonArray.getJSONObject(ii);
                    String totalDate = jsonObject.getString("totalDate");
                    Integer totalAmount = jsonObject.getInteger("totalAmount");
                    returnMap.put(totalDate,totalAmount.doubleValue());
                }
            }
        }
        cdpUtilsBo.setGroupLineData(returnMap);
    }

}
